package  
{
    import org.flixel.*;
	import Resources;
	/**
     * ...
     * @author Richard Pan
     */
    public class ScoreScreen extends FlxState 
    {
        
        private var levelSelect:LevelSelect;
        private var levelNumber:Number;

        // Button Backdrops
        [Embed(source = "ButtonBG.png")]
        private var nextButtonBG:Class;
        
        [Embed(source = "ButtonBG.png")]
        private var replayButtonBG:Class;
        
        [Embed(source = "ButtonBG.png")]
        private var menuButtonBG:Class;
		
		private var ratingList:LevelRating;
		private var ratingStar:FlxSprite;
		
        
        private var buttons:Array;
        private var nextButton:FlxIdxBtn;
        private var replayButton:FlxIdxBtn;
        private var menuButton:FlxIdxBtn;
        
        private var ls:LevelSelect;
        private var gameInfo:GameProgress;
        
        private var titleText:FlxText;
        private var detailsText:FlxText;
        
        private var ink:Number;
        private var time:Number;
        
        // Background image
        private var background:Backdrop;
		
		// User Select
		private var selected:int;
        
        private var NUM_LEVELS:Number = 20;
		
        
        public function ScoreScreen(levelSelection:LevelSelect, levelNum:Number, gameData:GameProgress, inkCount:Number, elapsedTime:Number)
        {
            super();
			
			ratingList = new LevelRating();
            levelSelect = levelSelection;
            levelNumber = levelNum;
            gameInfo = gameData;
            
            ink = inkCount;
            time = elapsedTime;
			selected = 2;

            
            ls = new LevelSelect(null);
            ls.setGameProgress(gameInfo);
            
            SaveDataManager.saveLevelData(levelNum, inkCount, elapsedTime);
        }
        
        override public function create():void {
            // FlxG.bgColor = 0xffffffff;
            background = new Backdrop(0, 0);
            add(background);
            
            titleText = new FlxText(270, 40, 240, "Level " + (levelNumber + 1) + ": You made it!", false);
			titleText.setFormat("Comic Sans MS", 18, 0x444444, "center", 0);
			add(titleText);
			
			
			ratingStar = new FlxSprite(365, 90, null);
			if (ink >= ratingList.levelRating[levelNumber + 1][0]) {
				ratingStar.loadGraphic(Resources.gold, false, false, 60, 60, false);
			} else if (ink >= ratingList.levelRating[levelNumber + 1][1]) {
				ratingStar.loadGraphic(Resources.silver, false, false, 60, 60, false);
			} else {
				ratingStar.loadGraphic(Resources.bronze, false, false, 60, 60, false);
			}
			ratingStar.scale.x = ratingStar.scale.y = 1.5;
			ratingStar.offset.x = ratingStar.offset.y = -5;
			add(ratingStar);
			
			
            
            detailsText = new FlxText(150, 200, 500, "Ink Count: " + ink + "\n Elapsed Time: " +
                                                    FlxU.formatTime(time)/*int(time * 1000) / 1000*/, false);
			detailsText.setFormat("Comic Sans MS", 24, 0x444444, "center", 0);
			add(detailsText);
            
            buttons = new Array();
            
            replayButton = new FlxIdxBtn(80, 370, "Replay level", levelSelect.btnClick, levelNumber);
            replayButton.loadGraphic(replayButtonBG, false, false, 160, 80, false);
            replayButton.label.offset.y = -15;
			replayButton.label.offset.x = -40;
			replayButton.width = 160;
            replayButton.label.setFormat(null, 13, 0xffffff, "center", 0);
            buttons.push(replayButton);
            
            menuButton = new FlxIdxBtn(560, 370, "Level selection", FlxG.switchState, ls);
            menuButton.loadGraphic(menuButtonBG, false, false, 160, 80, false);
            menuButton.label.offset.y = -15;
			menuButton.label.offset.x = -40;
			menuButton.width = 160;
			menuButton.label.width = 200;
            menuButton.label.setFormat(null, 13, 0xffffff, "center", 0);
            buttons.push(menuButton);
            
            add(buttons[0]);
            add(buttons[1]);
            
            FlxG.log(levelNumber);
            if (levelNumber < NUM_LEVELS - 1) {
                nextButton = new FlxIdxBtn(320, 370, "Next level\n(Space)", levelSelect.btnClick, levelNumber + 1);
                nextButton.loadGraphic(nextButtonBG, false, false, 160, 80, false);
                nextButton.label.offset.y = -10;
				nextButton.label.offset.x = -40;
				nextButton.scale.x = nextButton.scale.y = 1.5;
                nextButton.label.setFormat(null, 13, 0xffffff, "center", 0);
                buttons.push(nextButton);
                add(buttons[2]);
            }
            FlxG.mouse.show();
        }
		
		override public function update():void {
			// Keyboard shortcuts for selecting buttons
			if (FlxG.keys.justPressed("SPACE") && levelNumber < NUM_LEVELS - 1) {
				levelSelect.btnClick(levelNumber + 1);
			}
			if (FlxG.keys.justPressed("ESCAPE")) {
				FlxG.switchState(ls);
			}
			if (FlxG.keys.justPressed("R")) {
				levelSelect.btnClick(levelNumber);
			}
			
			
			super.update();
			
		}
    }

}